﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Crank.Core
{
    class TypeAliasTransformer : IModelTransformer
    {
        #region Blackboard Variables

        // Name, Type
        private static readonly IProperty<IDictionary<string, string>> TypeAliases = Blackboard.Create<IDictionary<string, string>>("TypeAliases");

        #endregion

        #region IModelTransformer Members

        public void ApplyTransform(DataModel target)
        {
            foreach (var entity in target.Entities.Values)
                foreach (var att in entity.Attributes)
                    UpdateTypeFromAlias(att);
        }

        #endregion

        private void UpdateTypeFromAlias(AttributeDef att)
        {
            if (TypeAliases.Value.ContainsKey(att.Type.ToLower()))
                att.Type = TypeAliases.Value[att.Type.ToLower()];
        }
    }
}
